<?php
/*
Plugin Name: Get Comments Count
Plugin URI: http://tantannoodles.com/toolkit/get-comments-count/
Description: Inserts a small image into your posts which displays the number of comments a post has.
Version: 0.21
Author: Joe Tan
Author URI: http://tantannoodles.com/about/

Copyright (C) 2007  Joe Tan

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

*/

class TanTanGetComments {

    function getComments($postId) {
    global $wpdb;
        $postId = (int) $postId;
        if ($postId) {
        	$res = $wpdb->get_row('select comment_count as count from '. $wpdb->posts .' WHERE ID = \''. addslashes($postId) .'\'');
        	$numComments = $res->count;
        } else {
        	// invalid post ID
        	$numComments = -1;
        }
        return (int) $numComments;
        
    }
    function wp_head() {
        remove_filter('the_content', array(&$this, 'addNumCommentsImage'));
    }

    function printNumCommentsImage() {
        if (function_exists('imagecreate')) {
        	$num = $this->getComments($_GET['p']);
        	$path = '/cache/get-comments/'.$num.'.gif';
        	$file = dirname(__FILE__).$path;
        	if (!file_exists($file)) {
        	    $this->setupDirs();
            	$image = imagecreate(100, 15);
            	$text = "(" . $num . (($num == 1) ? " comment" : " comments"). ")";
            	$bg = imagecolorallocate($image, 255, 0, 0);
            	imagefill ($image, 0, 0, $bg);
            	imagecolortransparent($image, $bg);
            	$color = imagecolorallocate($image, 0, 0, 0);
            	imagestring($image, 2, 0, 0, $text, $color);
            	imagegif($image, $file);
            	imagedestroy($image);
        	}
        	$url = dirname($_SERVER['REQUEST_URI']).$path;
        	header("Location: $url");
        	exit;
        
        } else {
            echo "Error: PHP <a href='http://us2.php.net/manual/en/ref.image.php'>image library</a> does not seem to be installed.";
        }
    }

    function addNumCommentsImage($content = '') {
        global $id;
        $img = 
            '<div class="tantan-getcomments"><a href="'.get_permalink($id).'#comments"><img src="'.
            get_bloginfo('url').'/wp-content/plugins/tantan/get-comments.php?p='.$id.
            '" width="100" height="15" style="border:0;" /></a></div>';
        return $content . $img;
    }
    function setupDirs() {
    	if (!file_exists(dirname(__FILE__).'/cache/get-comments/')) {
    	    mkdir(dirname(__FILE__).'/cache/', 0775);
            mkdir(dirname(__FILE__).'/cache/get-comments/', 0775);
	    }

    }
    function TanTanGetComments() {
        // called directly
        if(ereg('get-comments\.php\?p\=',$_SERVER["REQUEST_URI"])>0) {
            $this->printNumCommentsImage();
        } else {
            add_filter('the_excerpt_rss', array(&$this, 'addNumCommentsImage'));
            
            //
            // Comment out these two lines if you don't republish your posts via publish_post API hook
            //
            add_filter('the_content', array(&$this, 'addNumCommentsImage'));
            add_action('wp_head', array(&$this, 'wp_head'));
            add_action('activate_tantan/get-comments.php', array(&$this, 'setupDirs'));
        }

    }
}
require_once(dirname(__FILE__).'/../../../wp-config.php');
$TanTanGetCommentsPlugin =& new TanTanGetComments();
?>
